const router = require("koa-router")();
const DB = require("../../module/db");
const sd = require("silly-datetime");
const crypto = require("crypto");

router.get("/", async (ctx) => {
    ctx.body = "review首页";

}).post("/addReview", async (ctx) => {
    let data = ctx.request.body;
    let createTime = new Date();
    let list = {
        "name": data.name,
        "userId": data.userId,
        "mediaId": [],
        "introduction": data.introduction,
        "link": ''
    }
    let result1 = await DB.insert('review', list);
    let result2 = await DB.addArray("project", {"_id": DB.getObjectId(data.projectId)}, {"reviews": result1.ops[0]._id})
    let time = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
    let result3 = await DB.update("review", {"_id": DB.getObjectId(result1.ops[0]._id)}, {"link": cryptId(result1.ops[0]._id.toString())})
    if (result1.result.ok && result2.result.ok && result3.result.ok) {
        // console.log(result1.ops[0]._id)
        let dataFindResult = await DB.find('review', {"_id": DB.getObjectId(result1.ops[0]._id)});
        ctx.body = {
            type: 1,
            info: dataFindResult
        };
        console.log(time + "：添加审验列表成功" + data.name);
    } else {
        ctx.body = {
            type: 0,
            info: "添加审验列表失败"
        };
    }

}).post("/getReviews", async (ctx) => {
    let list = ctx.request.body.reviews
    let list2 = []
    for (let i = 0; i < list.length; i ++) {
        let data = await DB.find('review', {"_id": DB.getObjectId(list[i])})
        list2.push(data[0])
    }
    ctx.body = list2;

}).post("/deleteReview", async (ctx) => {
    let data = ctx.request.body
    let mediaIdList = ctx.request.body.mediaId
    let flag = true
    for (let i = 0; i < mediaIdList.length; i ++) {
        let data = await DB.findSomeCol('media', {'_id': DB.getObjectId(mediaIdList[i])}, {"_id": 0, "fileId": 1});
        let fileId = data[0].fileId;
        let data1 = await DB.remove1('media', {"_id": DB.getObjectId(mediaIdList[i])});
        if (data1.result.ok) {
            let data2 = await DB.remove1('fs.files', {"_id": DB.getObjectId(fileId)});
            let data3 = await DB.remove2('fs.chunks', {"files_id": DB.getObjectId(fileId)});
            if (data2.result.ok && data3.result.ok) {
                let data4 = await DB.remove2('comment', {"mediaId": mediaIdList[i]});
                let data5 = await DB.findSomeCol('tagging', {'mediaId': mediaIdList[i]}, {"_id": 0, "fileId": 1});
                if (data5.length > 0) {
                    let fileId2 = data5[0]["fileId"];
                    let data6 = await DB.remove1('fs.files', {"_id": DB.getObjectId(fileId2)});
                    let data7 = await DB.remove2('fs.chunks', {"files_id": DB.getObjectId(fileId2)});
                    let data8 = await DB.remove1('tagging', {'mediaId': mediaIdList[i]});
                    if (!(data6.result.ok && data7.result.ok && data8.result.ok))
                        flag = false
                }
                flag = flag && data4.result.ok
            } else
                flag = false
        } else
            flag = false
    }
    if (flag) {
        let result1 = await DB.remove1('review', {"_id": DB.getObjectId(data.id)})
        let result2 = await DB.deleteArray('project', {"_id": DB.getObjectId(data.projectId)}, {"reviews": DB.getObjectId(data.id)})
        let time = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
        if (result1.result.ok && result2.result.ok) {
            ctx.body = {
                type: 1,
                info: "删除审验列表成功"
            };
            console.log(time + "：删除审验列表成功");
        } else {
            ctx.body = {
                type: 0,
                info: "删除审验列表失败"
            };
        }
    } else {
        ctx.body = {
            type: 0,
            info: "删除审验列表失败"
        };
    }

}).post("/updateReview", async (ctx) => {
    let data = ctx.request.body;
    let time = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
    if (data.type === "info") {
        let list = {
            "name": data.name,
            "introduction": data.introduction
        }
        let result = await DB.update("review", {"_id": DB.getObjectId(data.id)}, list)
        if (result.result.ok) {
            // console.log(result.ops[0]._id)
            ctx.body = 1;
            console.log(time + "：修改审验列表成功" + data.name);
        } else {
            ctx.body = "修改审验列表失败";
        }
    }

}).post("/getReview", async (ctx) => {
    let reviewId = ctx.request.body.reviewId;
    let data = await DB.find("review", {"_id": DB.getObjectId(reviewId)})
    ctx.body = data[0]

});

function cryptId(id) {
    let md5 = crypto.createHash('md5');
    return md5.update(id).digest('hex');
}

global.cryptId = cryptId;
module.exports = router.routes();
